package com.yeyks.other.service.impl;

import com.yeyks.other.dal.domain.StatisticsRequest;
import com.yeyks.other.dal.dao.StatisticsRequestMapper;
import com.yeyks.other.service.StatisticsRequestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 请求路径次数记录 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-06-25
 */
@Service
public class StatisticsRequestServiceImpl extends ServiceImpl<StatisticsRequestMapper, StatisticsRequest> implements StatisticsRequestService {
    @Autowired
    private StatisticsRequestMapper statisticsRequestMapper;

    //根据路径列表更新数据库数据列表
    @Override
    public void updateData(ArrayList<String> allUrlPathList) {
        //查询所有对象
        List<StatisticsRequest> statisticsRequests = statisticsRequestMapper.selectList2();
        //创建已存在的路径对象
        ArrayList<String> datasourceDataList = new ArrayList<>();
        if (statisticsRequests != null) {
            statisticsRequests.forEach(adminStatisticsRequest -> datasourceDataList.add(adminStatisticsRequest.getPath()));
        }
        //获取添加列表
        ArrayList<String> addList = new ArrayList<>();
        addList.addAll(allUrlPathList);
        addList.removeAll(datasourceDataList);
        //添加
        if (addList.size() > 0) {
            for (String path : addList) {
                StatisticsRequest adminStatisticsRequest = new StatisticsRequest();
                adminStatisticsRequest.setPath(path);
                adminStatisticsRequest.setTimes(0L);
                statisticsRequestMapper.insert(adminStatisticsRequest);
            }
        }
        //获取删除列表
        ArrayList<String> removeList = new ArrayList<>();
        removeList.addAll(datasourceDataList);
        removeList.removeAll(allUrlPathList);
        //删除
//        if (removeList.size() > 0) {
//            adminStatisticsRequestExample.clear();
//            adminStatisticsRequestExample.createCriteria().andPathIn(removeList);
//            statisticsRequestMapper.deleteByExample(adminStatisticsRequestExample);
//        }
    }

    //根据路径更新次数
    @Override
    public int increaseTimesByPath(String path) {
        int result = statisticsRequestMapper.increaseTimesByPath(path);
        return result;
    }
}
